//
//  JHMyColletionViewController.m
//  Liberty Voice
//
//  Created by dlios on 15-5-27.
//  Copyright (c) 2015年 ClearLove. All rights reserved.
//

#import "JHMyColletionViewController.h"

#import "MyCollectionViewCell.h"
#import "JHmodle.h"
//#import "SecondViewController.h"
#import "NetwordHandler.h"
#import "JHButtonViewController.h"
#import "NetwordHandler.h"


@interface JHMyColletionViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic,retain)UICollectionView *collectionView;
@property(nonatomic, retain)UIImageView *imageView;
@property (nonatomic, retain) NSMutableArray *arr;

@end

@implementation JHMyColletionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self dian];
    self.title = @"分类";
    [self getData];
}

- (void)getData
{
    [NetwordHandler requestWithURL:@"http://mobile.ximalaya.com/m/super_explore_index2?picVersion=5&scale=2" completion:^(id result) {
        NSDictionary *dic = result;
        self.arr = [NSMutableArray array];
        NSMutableArray *a = [[dic objectForKey:@"categories"] objectForKey:@"data"];
        for (NSDictionary *m in a) {
            JHmodle *aaa = [[JHmodle alloc] initWithDic:m];
            [_arr addObject:aaa];
        }
        [self craet];
        [self.indicatorView removeFromSuperview];
    }];
    
}

- (void)craet
{
//    self.view.backgroundColor = [UIColor whiteColor];
    
    //1.设置collectionView布局信息
    //苹果系统提供的一个瀑布流效果
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    //layout设置
    //item大小
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width / 5, self.view.frame.size.width / 5 + 40);
    //    //item之间最小行间距
    //    flowLayout.minimumLineSpacing = 10;
    //    //item之间最小列间距
    //    flowLayout.minimumInteritemSpacing = 10;
    //    //滑动方向(默认为垂直滑动)
    //    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //    //item的边界大小 (上左下右)
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    //2.1创建collectionView
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    //2.2设置代理
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    //2.3添加父视图
    [self.view addSubview:self.collectionView];
    //2.4内存管理
    [_collectionView release];
    [flowLayout release];
    //#warning collectionView创建时必须先注册一个cell
    [self.collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"my"];
    
    //导航左侧按钮
    //    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(leftAction:)]autorelease];
    //搜索框
    //    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(30, 0, 300, 30)];
    //    textField.placeholder = @"请输入要搜索的文字";
    //    textField.enabled = YES;
    //    self.navigationItem.titleView = textField;
    //    //开始输入的时候清除内容 clearOnBeginEditing
    //    textField.clearsOnBeginEditing = YES;
    //    //清除按钮样式clearButtonMode
    //    textField.clearButtonMode = UITextFieldViewModeAlways;
    //    //输入框样式 borderStyle
    //    textField.borderStyle = UITextBorderStyleRoundedRect;
    //    [textField release];
}
//导航左侧按钮方法
//- (void)leftAction:(UIBarButtonItem *)left
//{
//    SecondViewController *jhmiVC = [[SecondViewController alloc]init];
//    [self.navigationController pushViewController:jhmiVC animated:YES];
//}
//#pragma mark - collectionView 协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"my" forIndexPath:indexPath];
    cell.model = _arr[indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    JHButtonViewController *jhDeVC = [[JHButtonViewController alloc]init];
    jhDeVC.str = [_arr[indexPath.row] name];
    jhDeVC.dataId = [_arr[indexPath.row] orderNum];
    [self.navigationController pushViewController:jhDeVC animated:YES];
    //    NSLog(@"______________%ld", _id);
    
    
    //    detail.article_id = [[_ArrChina objectAtIndex:indexPath.row] article_id];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
